package com.sanpu.kxe.base;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;

import com.sanpu.kxe.R;
import com.sanpu.kxe.common.interfaces.DialogViewListener;
import com.sanpu.kxe.utils.AppContextUtil;
import com.sanpu.kxe.utils.DialogUtil;
import com.sanpu.kxe.utils.annotation.ModuleAnnotation;
import com.sanpu.kxe.utils.permissions.PermissionsConstant;

/**
 * Description：公共跳转功能
 * Created by 薛勇军 on 2018/6/8.
 */
@ModuleAnnotation(module = "kxeprovider")
public class CommonProvider implements ICommonProvider {

    @Override
    public void openAppPermission(final Activity activity, String permission) {
        if (activity != null && !activity.isFinishing()) {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(activity.getResources().getString(R.string.permission_prompt_start));
            switch (permission) {
                case PermissionsConstant.WRITE_STORAGE:
                case PermissionsConstant.READ_STORAGE:
                    stringBuffer.append("存储空间");
                    break;
                case PermissionsConstant.READ_PHONE_STATE:
                    stringBuffer.append("电话");
                    break;
                case PermissionsConstant.CAMERA:
                    stringBuffer.append("相机");
                    break;
                default:
                    return;
            }
            stringBuffer.append(activity.getResources().getString(R.string.permission_prompt_end));
            DialogUtil.getInstance().showDialogPromptByTwoBtnHaveCallBack(activity,
                    activity.getResources().getString(R.string.permission_title),
                    stringBuffer.toString(), activity.getResources().getString(R.string.cancel),
                    activity.getResources().getString(R.string.permission_setting), new DialogViewListener() {
                        @Override
                        public void btnRightConfirm() {

                            openSystemSetting(activity);
                        }

                        @Override
                        public void btnLeftCancel() {

                            exitApp(activity);
                        }
                    });
        }
    }

    @Override
    public void exitApp(Activity activity) {
        int currentVersion = android.os.Build.VERSION.SDK_INT;
        if (currentVersion > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
            Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            activity.startActivity(startMain);
            System.exit(0);
        } else {// android2.1
            ActivityManager am = (ActivityManager) activity
                    .getSystemService(Context.ACTIVITY_SERVICE);
            am.restartPackage(activity.getPackageName());
        }
    }

    @Override
    public void openSystemSetting(Activity activity) {
        Uri packageURI = Uri.parse("package:" + AppContextUtil.getPackageName(activity));
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
        activity.startActivity(intent);
    }
}
